package com.lys.emsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lys.emsystem.entity.ChartData;
import com.lys.emsystem.entity.Teach;
import com.lys.emsystem.entity.Teacher;
import com.lys.emsystem.mapper.IntentMapper;
import com.lys.emsystem.mapper.TeacherMapper;
import com.lys.emsystem.service.IStudentService;
import com.lys.emsystem.service.ITeachService;
import com.lys.emsystem.service.ITeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lys.emsystem.utils.Result;
import com.lys.emsystem.vo.StuJobVo;
import com.lys.emsystem.vo.TeacherVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liyansheng
 * @since 2022-04-27
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements ITeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private ITeachService teachService;

    @Autowired
    private IStudentService studentService;

    @Autowired
    private IntentMapper intentMapper;

    @Override
    public Teacher selectTeacherInfoById(Integer userId) {
        return teacherMapper.selectTeacherById(userId);
    }
    @Override
    public IPage<Teacher> search(Page<Teacher> page, TeacherVo teacherVo) {
        return teacherMapper.search(page, teacherVo);
    }

    @Override
    public List<StuJobVo> getStuJobStatus(Integer userId) {
        return teacherMapper.getStuJobStatus(userId);
    }

    @Override
    public Result getAnalyzeData(Integer userId) {
        HashMap<String, Object> map = new HashMap<>();
        //获取班级数
        QueryWrapper<Teach> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id", userId);
        List<Teach> list = teachService.list(wrapper);
        map.put("classes", list.size());
        //获取该老师下的签约人数
        Integer signPeople=0;
        List<ChartData> chartDataList= teacherMapper.selectEmpStatusIsTrue(userId);
        for (ChartData chartData : chartDataList) {
            if (chartData.getName().equals("1")){
                chartData.setName("已签约");
                signPeople = chartData.getValue();
            }
            if (chartData.getName().equals("0")){
                chartData.setName("未签约");
            }
        }
        map.put("signPeople", signPeople);
        map.put("haveSigned", chartDataList);
    //    获取教师下的总学生数
        int total = 0;
        for (ChartData chartData : chartDataList) {
            total = total + chartData.getValue();
        }
        map.put("totalPeople", total);
        // 获取男女比例
        List<ChartData> StudentSex = studentService.selectStudentSexCount(userId);
        for (ChartData studentSex : StudentSex) {
            if (studentSex.getName().equals("1")) {
                studentSex.setName("男生");
            }
            if (studentSex.getName().equals("0")) {
                studentSex.setName("女生");
            }
        }
        map.put("sexRatio", StudentSex);

        //获取生源地分布
        List<ChartData> studentProvince = studentService.selectStudentProvince(userId);
        for (ChartData chartData : studentProvince) {
            String name=chartData.getName();
            if (name.equals("内蒙古自治区")||name.equals("黑龙江省")) {
                chartData.setName(name.substring(0, 3));
            } else {
                chartData.setName(name.substring(0, 2));
            }
        }
        map.put("provinces", studentProvince);
        return Result.buildResult(Result.Status.OK, map);
    }

    @Override
    public Result getAnalyzeIntent(Integer userId) {
        HashMap<String, Object> map = new HashMap<>();
        List<ChartData> salaryChartData = intentMapper.getSalaryChartData();

        ArrayList<Integer> salaryArray = new ArrayList<>();
        for (ChartData salaryChartDatum : salaryChartData) {
            salaryArray.add(salaryChartDatum.getValue());
        }
        map.put("salaryData", salaryArray);
        List<ChartData> intentChartData=intentMapper.getIndustryData(userId);
        map.put("industryData", intentChartData);
        List<ChartData> intentCityData = intentMapper.getIntentCityData(userId);
        for (ChartData chartData : intentCityData) {
            String name=chartData.getName();
            if (name.equals("内蒙古自治区")||name.equals("黑龙江省")) {
                chartData.setName(name.substring(0, 3));
            } else {
                chartData.setName(name.substring(0, 2));
            }
        }
        map.put("cityData", intentCityData);
        List<ChartData> intentPostData = intentMapper.getIntentPostData(userId);
        map.put("postData", intentPostData);
        return Result.buildResult(Result.Status.OK, map);
    }

}
